//
//  WBComposeBottomView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBComposeBottomView.h"
#import <Masonry.h>

@interface  WBComposeBottomView()

@property (nonatomic , strong) UIButton *emoticonBtn;

@end


@implementation WBComposeBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
    
        [self setupUI];
    }
    
    return self;

}

- (void)setIsEmoticon:(BOOL)isEmoticon
{
    
    NSString *imageName;
    
    if(isEmoticon)
    {
        //是自定义view 这个时候Btn的图片应该是键盘图片
        imageName = @"compose_keyboardbutton_background";
    
    }else
    {
        //不是自定义view btn的图片应该是笑脸
        imageName = @"compose_emoticonbutton_background";
    
    }
    
    [self.emoticonBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.emoticonBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] forState:UIControlStateHighlighted];
    
}



#pragma mark - 设置UI
- (void)setupUI
{
    
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
    
    
    //设置约束
    UIButton *pictureBtn = [self addChildButtonWithImageName:@"compose_toolbar_picture" andTag:Picture];
    UIButton *mentionBtn = [self addChildButtonWithImageName:@"compose_mentionbutton_background" andTag:Mention];
    UIButton *trendBtn = [self addChildButtonWithImageName:@"compose_trendbutton_background" andTag:Trend];
    self.emoticonBtn = [self addChildButtonWithImageName:@"compose_emoticonbutton_background" andTag:Emoticon];
    UIButton *addBtn = [self addChildButtonWithImageName:@"compose_add_background" andTag:Add];
    
    [pictureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(mentionBtn);
        
    }];
    [mentionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.leading.mas_equalTo(pictureBtn.mas_trailing);
        make.width.mas_equalTo(trendBtn);
    }];
    [trendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.leading.mas_equalTo(mentionBtn.mas_trailing);
        make.width.mas_equalTo(self.emoticonBtn);
    }];
    [self.emoticonBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.leading.mas_equalTo(trendBtn.mas_trailing);
        make.width.mas_equalTo(addBtn);
    }];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.bottom.mas_equalTo(self);
        make.leading.mas_equalTo(self.emoticonBtn.mas_trailing);
    }];
    
}


- (UIButton *)addChildButtonWithImageName:(NSString *)imageName andTag:(HMComposeBottomViewType)type
{
    //创建btn compose_add_background_highlighted
    UIButton *btn = [[UIButton alloc] init];
    
    btn.tag = type;
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] forState:UIControlStateHighlighted];
    
    //添加控件
    [self addSubview:btn];
    
    return btn;

}

#pragma mark - 按钮监听方法的实现
- (void)btnClick:(UIButton *)btn
{
    //执行block
    self.block((HMComposeBottomViewType)btn.tag);
}


@end
